import logging

import pytest

from resource.pom.gallery import Gallery
from resource.pom.launcher import Launcher
from resource.pom.settings import Settings


class Test:
    app = Settings
    gallery = Gallery

    @pytest.mark.parametrize('setup_teardown', [app.bundle_name], indirect=True)
    def test(self, setup_teardown, device):
        logging.info('步骤1：进入桌面，点击桌面中任意一个应用')
        device.start_ability(self.app.bundle_name, self.app.ability_name)
        device.refresh_layout()
        mobile_network = device.get_element_by_text('移动网络')
        device.click_element(mobile_network)
        logging.info('预期结果1：成功进入任意应用')
        device.assert_current_window(self.app.window_name)
        device.refresh_layout()
        device.assert_text_exist('移动数据')

        logging.info('步骤2：点击左下角back键')
        device.go_back()
        device.refresh_layout()
        logging.info('预期结果2：点击back键可以正常返回至上一级页面')
        device.assert_text_exist('设置')
        device.assert_text_exist('移动网络')

        logging.info('步骤3：点击底部home键')
        device.go_home()
        device.wait(1)
        logging.info('预期结果3：点击home键可以正常返回至桌面')
        device.assert_current_window(Launcher.entry_view)

        logging.info('步骤4：点击右下角recent键')
        device.start_ability(self.gallery.bundle_name, self.gallery.ability_name)
        device.wait(2)
        device.go_home()
        device.press_recent_key()
        logging.info('预期结果4：多个应用展示后台运行')
        device.refresh_layout()
        device.assert_text_exist('图库')
        device.assert_text_exist('设置')

        logging.info('步骤5.1：上滑清理某一个后台运行的应用')
        device.swipe(180, 640, 180, 320)
        device.wait(1)
        logging.info('预期结果5.1：上滑可结束某一个后台运行的应用')
        windows = device.dump_windows_manager_service()
        assert (self.app.window_name not in windows or self.gallery.window_name not in windows), '后台应用清理失败'

        logging.info('步骤5.2：点击垃圾桶按钮')
        device.press_clear_btn()
        device.wait(1)
        logging.info('预期结果5.2：点击垃圾桶按钮可清理所有后台运行的应用')
        windows = device.dump_windows_manager_service()
        assert self.app.window_name not in windows, '后台应用清理失败'
        assert self.gallery.window_name not in windows, '后台应用清理失败'
